/**
 * 读取boot文件并写入到system.img文件，再在system.img后追加一些特殊字节。
 */
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class OperatingSystem {
    //private int[] imgContent = new int[] {};
    private ArrayList<Integer> imgByteToWrite = new ArrayList<Integer>();
    private void readKernelFromFile(String fileName) {
        File file = new File(fileName);
        InputStream in = null;

        try {
            in = new FileInputStream(file);
            int tempbyte;
            while((tempbyte = in.read()) != -1) {
                imgByteToWrite.add(tempbyte);
            }
        } catch(IOException e) {
            e.printStackTrace();
            return;
        }

        imgByteToWrite.add(0x55);
        imgByteToWrite.add(0xaa);
//        imgByteToWrite.add(0xf0);
//        imgByteToWrite.add(0xff);
//        imgByteToWrite.add(0xff);
    }

    // 参数s并没有使用
    public OperatingSystem(String s) {
        readKernelFromFile("boot");
        // 一个软盘有两个盘面，每个盘面有18个柱面，每个柱面有80个扇区，每个扇区512字节
        // 所以一个软盘容量大小为：2*18*80*512=1474560=0x168000 Bytes
        int len = 0x168000; 
        int curSize = imgByteToWrite.size();
        for(int l = 0; l < len - curSize; l++) {
            imgByteToWrite.add(0);
        }
    }

    public void makeFlopy() {
        try {
            DataOutputStream out = new DataOutputStream(new FileOutputStream("system_ch02.img"));
            for(int i = 0; i < imgByteToWrite.size(); i++) {
                out.writeByte(imgByteToWrite.get(i).byteValue());
            }
        } catch(Exception e) {
            // TODO: auto-generated cated block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        OperatingSystem op = new OperatingSystem("");
        op.makeFlopy();
    }
}
